import React from 'react'
// ===================================================================== Temp
import Tabs from './cpt/tabs'
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy, globalConfig } = window
const { COVIDUrl } = globalConfig
// ===================================================================== $Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const Box 				= $Lazy.load(()=>import('#tp/box/box'))
const BtnGroup 			= $Lazy.load(()=>import('#tp/btn-group'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const $time 			= $Lazy.file(()=>import('@com/utils/time'))
const InfoTable 		= $Lazy.state(()=>import('./cpt/info-table'))
// ===================================================================== data
const tabsData = [
	{ title:'结果录入', 	status:0 },
	{ title:'已提交', 	status:1 },
	{ title:'已迟发', 	status:2 },
	{ title:'已退单', 	status:-1 },
	{ title:'已终止', 	status:-2 },
]
const AuditStatus = [{ name:'全部', value:-99 },{ name:'待检', value:0 },{ name:'已检', value:1 },{ name:'迟发', value:2 },{ name:'退单', value:-1 },{ name:'终止', value:-2 }]
const ReportStatus = [{ name:'无报告', value:0 },{ name:'已出报告', value:1 }]
// ===================================================================== component
class Index extends React.Component{
	state={
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		forms: [
			{ label: '条码号',			name: 'spec_code'  },
			{ label: '实验号',			name: 'experiment_num' },
			{ label: '接收时间',			names: ['check_time_start','check_time_end'], name:'check_time', type: 'date-range', loading:true }, // 录入、迟发、退单、终止
			{ label: '检测时间',			names: ['test_time_start','test_time_end'], name:'test_time', type: 'date-range', loading:true, hide:true },
			{ label: '医院名称',			name: 'hosp_id', type: 'select', loading:true,	idStr:'uuid',	nameStr:'hospName' },
			{ label: '板号',			name: 'board_no' },
			{ label: '状态',			name: 'had_report',	type: 'select',	data:ReportStatus},
		],
	}

	componentDidMount(){
		this.tabs = tabsData
		this.index = $fn.local('covidTabs') || 0
		this.status = this.tabs[this.index]['status']
		// this.getSelect()
	}
	
	initTime = (data) => {
		const searchValue= this.searchFormRef.getValue()
		if($fn.hasObject(data)){
			const {name, names=[]} = data
			$time.then(f=>{
				const arr = f.duration['2-9-now']()
				this.model = {...searchValue,[names[0]]:arr[0], [names[1]]: arr[1]}
				this.searchFormRef.setValue({[name]: arr})
				this.searchFormRef.submit()
			})
		}else{
			this.model = searchValue
			this.searchFormRef.submit()
		}
		this.setState({selectedKeys: [], selectedRows: []})
	}
	
	getSearch = (status) => {
		const { forms } = this.state
		if(status === 0){ // 接收时间
			this.timeName = 'check_time'
			$fn.hideRows(forms, 'check_time', false)
			$fn.hideRows(forms,'test_time', true)
			this.initTime({name: 'check_time', names: ['check_time_start', 'check_time_end']})
		}else if(status === 1){ // 检测时间
			this.timeName = 'test_time'
			$fn.hideRows(forms, 'test_time', false)
			$fn.hideRows(forms, 'check_time', true)
			this.initTime({name: 'test_time', names: ['test_time_start', 'test_time_end']})
		}else{
			this.timeName = 'jz_time'
			$fn.hideRows(forms, [ 'check_time', 'test_time' ], true)
			this.initTime()
		}
	}
	
	getSelect(){
		const {forms} = this.state
		return $cache.then( async f => {
			// 医院名称
			this.hostData = await f.getCache({ api:COVIDUrl+'Hospital/query',param:{page:1,page_num:10000,status:1} })
			$fn.setRow( forms, 'hosp_id', this.hostData )
			// 自然项目
			// this.projectData = await f.getCache({ api:'nature_project' })
			// 操作人
			this.userData = await f.getCache({ api: 'employee/select' })
			// 项目名称
			const settings = await f.getCache({ api: COVIDUrl+'settings/query', param: {page_num:1000,page:1} })
			this.projectName = settings.find(v => v.settingsCode === 'ConvidProjectName')

			this.setState({ forms })
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,COVIDUrl+'codeinfo/query', { COVID:true, param:{status:1,test_state:this.status} })
	
	cols = [
		{ title: '标本条码', 	dataIndex: 'specCode',  		width:130  },
		{ title: '项目名称', 	dataIndex: 'parentKindId', 		width:220,		render: text => `${this.projectName?.settingsValue ?? '--' }`},
		{ title: '医院名称', 	dataIndex: 'hospId', 			width:200,		render: text => $fn.getState(this.hostData,text,'hospName','uuid') },
		{ title: '板号', 		dataIndex: 'boardNo', 			width:100,		},
		{ title: '实验号', 		dataIndex: 'experimentNum', 	width:100,		},
		{ title: '是否出报告', 	dataIndex: 'hadReport', 		width:100,		render: text => $fn.getState(ReportStatus,text) },
		{ title: '操作人', 		dataIndex: 'testOperator', 		width:200,		render: text => $fn.getState(this.userData,text) },
		{ title: '项目状态', 	dataIndex: 'testState', 		width:80,		render: text => $fn.getState(AuditStatus,text) },
		{ title: '接收时间', 	dataIndex: 'checkTime',  		...$fn.table.time },
		{ title: '检测时间', 	dataIndex: 'testTime',  		...$fn.table.time },
		{ title: '备注', 		dataIndex: 'remark',  			...$fn.table.time },
	]
	
	controls = () => {
		const { selectedKeys } = this.state
		const uuids = selectedKeys.join(',')
		return [
			(+this.status===0||+this.status===2) && Confirm.call(this, '提交', COVIDUrl+'codeinfo/testState', { isKeys:1, query:{state:1,uuids} }),
			+this.status===0 && Confirm.call(this, '迟发', COVIDUrl+'codeinfo/testState', { isKeys:1, query:{state:2,uuids} }),
			+this.status===0 && Confirm.call(this, '退单', COVIDUrl+'codeinfo/testState', { isKeys:1, query:{state:-1,uuids} }),
			+this.status===1 && Confirm.call(this, '退回', COVIDUrl+'codeinfo/testState', { isKeys:1, query:{state:0,uuids} }),
			(+this.status===0||+this.status===2) && Confirm.call(this, '终止', COVIDUrl+'codeinfo/testState', { isKeys:1, query:{state:-2,uuids} }),
		]
	}
	
	tabChange = (status) => {
		this.status = +status
		this.getSearch(+status)
		this.infoTableRef.setState({data:[]})
	}
	
	render(){
		return (
			<>
				<Page nobc className='small-bm'>
					{/* Tabs */}
					<Tabs top={this} tabChange={this.tabChange} />
					{/* 操作按钮 */}
					<div className='bcf pt5 pl10'><BtnGroup controls={this.controls()} /></div>
					{/* 搜索 */}
					{ SearchForm.call(this,{timeStr:'2-9-now', onForm: async () => {
						await this.getSelect()
						await this.getSearch(+this.status)
					} }) }
					{/* 列表内容 */}
					<div className='ex mt5 fx h'>
						{/* 表格 */}
						<div className='h ex' style={{width:'35%'}}>
							<Box title= '检测项目' className	= 'ex fv h' contentClassName= 'fv pt10 ex h'>
								{ Table.call( this, {
									onRow: ( { specCode },index )=>{
										this.infoTableRef.query = {spec_code: specCode.replace(/(^\s*)|(\s*$)/g,"")}
										this.infoTableRef.fetch()
									}
								})}
							</Box>
						</div>
						<div className='h ex mlr5'>
							<InfoTable top={this} onRef={ref=>this.infoTableRef=ref} />
						</div>
					</div>
				</Page>
			</>
		)
	}
}
export default Index